package com.example.smart.ui.m

import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import androidx.drawerlayout.widget.DrawerLayout
import com.example.lib.constants.JumpConstants
import com.example.lib.extend.getIntentInt
import com.example.lib.extend.gone
import com.example.lib.extend.isVisi
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.utils.Logger
import com.example.lib.utils.ToastUtils
import com.example.lib.utils.am.ActManger
import com.example.lib.utils.sp.SpKey
import com.example.lib.utils.sp.SpUtils
import com.example.smart.R
import com.example.lib.utils.statusbar.StatusBarUtils
import com.example.smart.adapter.viewpager.ViewPagerAdapter
import com.example.smart.base.ui.activity.BaseActivity
import com.example.smart.base.ui.fragment.BaseFragment
import com.example.smart.databinding.ActivityMainMBinding
import com.example.smart.databinding.LayoutHeaderBinding
import com.example.smart.listener.OnChildScrollListener
import com.example.smart.ui.fragment.CourseFragment
import com.example.smart.ui.m.frag.MCourseFragment
import com.example.smart.ui.m.frag.MHomeFragment
import com.example.smart.utils.DisplayUtils

class MMainActivity : BaseActivity<ActivityMainMBinding>(), OnChildScrollListener {
    private var mFragments: MutableList<BaseFragment<*>> = mutableListOf()
    private lateinit var mHeaderBinding: LayoutHeaderBinding

    override fun layoutID() = R.layout.activity_main_m

    override fun afterOnGetBinding(savedInstanceState: Bundle?) {
        super.afterOnGetBinding(savedInstanceState)
        includeBinding.root.gone()
        mHeaderBinding = LayoutHeaderBinding.bind(binding.HomeLeftMenu.inflateHeaderView(R.layout.layout_header))
    }

    override fun initView() {
        Logger.e("${SpUtils.getBoolean(SpKey.IS_LOGIN, false)}")
        StatusBarUtils.setStateAndNavigationBar(this@MMainActivity, statusBarColor = com.example.lib.R.color.translate)

        mFragments = mutableListOf(MHomeFragment.getInstance(), MCourseFragment.getInstance())
        binding.MainPager.isUserInputEnabled = false
        binding.MainPager.adapter = ViewPagerAdapter(this@MMainActivity, mFragments)
        binding.MainPager.offscreenPageLimit = mFragments.size

        binding.root.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
    }

    override fun initListener() {
        binding.MainGroup.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.MainHome -> {
                    binding.MainPager.setCurrentItem(0, false)
                }

                R.id.MainStudy -> {
                    binding.MainPager.setCurrentItem(1, false)
                }
            }
        }

        mHeaderBinding.HeaderClose.setOnDoubleClickListener {
            binding.root.close()
        }

        mHeaderBinding.HeaderOrder.setOnDoubleClickListener {
            DisplayUtils.toDayTask(this@MMainActivity)
        }

        mHeaderBinding.HeaderChat.setOnDoubleClickListener {
            DisplayUtils.toChatList(this@MMainActivity)
        }

        mHeaderBinding.HeaderExit.setOnDoubleClickListener {
            DisplayUtils.toLoginChoose(this@MMainActivity)
        }

        mHeaderBinding.HeaderPrivacy.setOnDoubleClickListener {
            ToastUtils.showToast("暂未开发")
        }

        mHeaderBinding.HeaderFeedback.setOnDoubleClickListener {
            ToastUtils.showToast("暂未开发")
        }

        binding.MainManage.setOnDoubleClickListener {
            DisplayUtils.toMine(this@MMainActivity)
        }

        binding.MainCommunity.setOnDoubleClickListener {
            DisplayUtils.toCommunity(this@MMainActivity)
        }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        val index = intent?.getIntentInt(JumpConstants.HOME_INDEX) ?: -1
        if (index != -1) {
            if (index == 0 || index == 1) {
                binding.MainPager.currentItem = index
            } else if (index == 2) {
                DisplayUtils.toCommunity(this@MMainActivity)
            } else {
                DisplayUtils.toMine(this@MMainActivity)
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ActManger.appExit()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onScroll(flag: Boolean) {
        includeBinding.root.isVisi(flag)
        binding.MainGroup.isVisi(flag.not())
    }

    override fun openMenu() {
        binding.root.open()
    }

    override fun isNeedAllTab() = true
}